import { fileURLToPath, URL } from 'node:url';

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import {viteStaticCopy} from 'vite-plugin-static-copy';
import path from 'path';


// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    viteStaticCopy({
      targets: [
        // {
        //   src: 'src/utils/background.ts',
        //   dest: '.'
        // },
        {
          src: 'manifest.json',
          dest: '.'
        }
      ]
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
    extensions: ['.ts', '.js'],
  },
  build: {
    rollupOptions: {
      input: {
        main: path.resolve(__dirname, 'index.html'),
        background: path.resolve(__dirname, 'src/utils/background.ts')
      },
      output: {
        manualChunks: {
          'vendor-lodash': ['lodash'],
          'vendor-moment': ['moment'],
          'vendor-element-plus-scripts': ['element-plus'],
          'vendor-element-plus-styles': ['element-plus/dist/index.css'],
        },
        entryFileNames: '[name].js'
      },
      // external: ['express'] // 添加 express 到外部依赖
      inlineDynamicImports: true,
    },
    // cache: true,
    terserOptions: {
      compress: {
        // // 去除console.log和debugger语句，减少代码量
        // drop_console: true,
        drop_debugger: true,
      },
      mangle: true,
      // // 并行压缩，提高速度
      // parallel: true,
    },
    minify: 'esbuild',
    // minify: 'terser',
    // // 添加以下配置，确保输出的代码是 ES6 模块格式
    // format: 'es',
  }
})
